/*
 * Lab# - Matlab interaction library for .Net
 * 
 * Copyright (C) 2005 Julien Roncaglia
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * THIS CODE IS GENERATED BY MxConvert.xsl FROM MxConvert.xml, 
 * DO NOT EDIT DIRECTLY.
 */
using System;
using System.Collections.Generic;
using System.Text;

namespace LabSharp
{
    public static partial class MxConvert
    {
        const string ONE_ELEMENT_REQUIRED = "A mxArray with only 1 element is required, but readed : {0}.";
        const string NO_WAY_TO_CAST = "Cannot cast a {0} mxArray to {1}.";
        const string NO_DOTNET_TYPE = "No corresponding .Net type exists for matlab's {0}.";

        /// <summary>
        /// Convert a mxArray to the corresponding type of array.
        /// </summary>
        /// <remarks>
        /// No conversion of type occurs, if the ClassID is Single, a float[] will be
        /// returned and never a double[].
        /// </remarks>
        static Array _ConvertToArray(MxArray array, ClassID classId, int ndims)
        {
            bool isComplex = array.IsComplex;
            switch (classId)
            {
                
                case ClassID.Char:
                    if (isComplex)
                    {
                        return _ToCharArray_Cplx(array);             
                    }
                    else
                    {
                        return _ToCharArray(array);             
                    }

                case ClassID.Logical:
                    if (isComplex)
                    {
                        return _ToBooleanArray_Cplx(array);             
                    }
                    else
                    {
                        return _ToBooleanArray(array);             
                    }

                case ClassID.Int8:
                    if (isComplex)
                    {
                        return _ToSByteArray_Cplx(array);             
                    }
                    else
                    {
                        return _ToSByteArray(array);             
                    }

                case ClassID.UInt8:
                    if (isComplex)
                    {
                        return _ToByteArray_Cplx(array);             
                    }
                    else
                    {
                        return _ToByteArray(array);             
                    }

                case ClassID.UInt16:
                    if (isComplex)
                    {
                        return _ToUInt16Array_Cplx(array);             
                    }
                    else
                    {
                        return _ToUInt16Array(array);             
                    }

                case ClassID.Int16:
                    if (isComplex)
                    {
                        return _ToInt16Array_Cplx(array);             
                    }
                    else
                    {
                        return _ToInt16Array(array);             
                    }

                case ClassID.UInt32:
                    if (isComplex)
                    {
                        return _ToUInt32Array_Cplx(array);             
                    }
                    else
                    {
                        return _ToUInt32Array(array);             
                    }

                case ClassID.Int32:
                    if (isComplex)
                    {
                        return _ToInt32Array_Cplx(array);             
                    }
                    else
                    {
                        return _ToInt32Array(array);             
                    }

                case ClassID.UInt64:
                    if (isComplex)
                    {
                        return _ToUInt64Array_Cplx(array);             
                    }
                    else
                    {
                        return _ToUInt64Array(array);             
                    }

                case ClassID.Int64:
                    if (isComplex)
                    {
                        return _ToInt64Array_Cplx(array);             
                    }
                    else
                    {
                        return _ToInt64Array(array);             
                    }

                case ClassID.Single:
                    if (isComplex)
                    {
                        return _ToSingleArray_Cplx(array);             
                    }
                    else
                    {
                        return _ToSingleArray(array);             
                    }

                case ClassID.Double:
                    if (isComplex)
                    {
                        return _ToDoubleArray_Cplx(array);             
                    }
                    else
                    {
                        return _ToDoubleArray(array);             
                    }

                default:
                    throw new InvalidCastException(string.Format(NO_DOTNET_TYPE, classId));
            }
        }

        /// <summary>
        /// Convert a mxArray to the corresponding basic object (Not an array).
        /// </summary>
        /// <remarks>
        /// No conversion of type occurs, if the ClassID is Single, a float will be
        /// returned and never a double.
        /// </remarks>        
        static Object _ConvertToBasicType(MxArray array, ClassID classId)
        {
            switch(classId)
            {
                
                case ClassID.Char:
                    if (array.IsComplex)
                    {
                        return _ToChar_Cplx(array);
                    }
                    else
                    {
                        return _ToChar(array);
                    }

                case ClassID.Logical:
                    if (array.IsComplex)
                    {
                        return _ToBoolean_Cplx(array);
                    }
                    else
                    {
                        return _ToBoolean(array);
                    }

                case ClassID.Int8:
                    if (array.IsComplex)
                    {
                        return _ToSByte_Cplx(array);
                    }
                    else
                    {
                        return _ToSByte(array);
                    }

                case ClassID.UInt8:
                    if (array.IsComplex)
                    {
                        return _ToByte_Cplx(array);
                    }
                    else
                    {
                        return _ToByte(array);
                    }

                case ClassID.UInt16:
                    if (array.IsComplex)
                    {
                        return _ToUInt16_Cplx(array);
                    }
                    else
                    {
                        return _ToUInt16(array);
                    }

                case ClassID.Int16:
                    if (array.IsComplex)
                    {
                        return _ToInt16_Cplx(array);
                    }
                    else
                    {
                        return _ToInt16(array);
                    }

                case ClassID.UInt32:
                    if (array.IsComplex)
                    {
                        return _ToUInt32_Cplx(array);
                    }
                    else
                    {
                        return _ToUInt32(array);
                    }

                case ClassID.Int32:
                    if (array.IsComplex)
                    {
                        return _ToInt32_Cplx(array);
                    }
                    else
                    {
                        return _ToInt32(array);
                    }

                case ClassID.UInt64:
                    if (array.IsComplex)
                    {
                        return _ToUInt64_Cplx(array);
                    }
                    else
                    {
                        return _ToUInt64(array);
                    }

                case ClassID.Int64:
                    if (array.IsComplex)
                    {
                        return _ToInt64_Cplx(array);
                    }
                    else
                    {
                        return _ToInt64(array);
                    }

                case ClassID.Single:
                    if (array.IsComplex)
                    {
                        return _ToSingle_Cplx(array);
                    }
                    else
                    {
                        return _ToSingle(array);
                    }

                case ClassID.Double:
                    if (array.IsComplex)
                    {
                        return _ToDouble_Cplx(array);
                    }
                    else
                    {
                        return _ToDouble(array);
                    }

                default:
                    throw new InvalidCastException(string.Format(NO_DOTNET_TYPE, classId));
            }
        }

        /// <summary>
        /// Try to convert a mxArray a specified .Net type.
        /// </summary>
        /// <remarks>
        /// <para>
        ///     A conversion could occurs, if you ask for a double[,] but the content is
        ///     a 2 dimensions mxArray of Single it will be converted to a double[,].
        /// </para>
        /// </remarks>        
        /// <param name="noVectorization">
        /// If this parametter is true and the user ask for a 1D array an exception will 
        /// be thrown if the mxArray isn't of dimension 1, otherwise it will vectorize the array.
        /// </param>
        static TType _ConvertToSomeType<TType>(MxArray array, ClassID classId, int ndims, bool noVectorization)
        {
            Type genericType = typeof(TType);
            Type convertToDataType;
            bool isArray;
            bool isComplexType;
            ExtractTypeInfos(genericType, out convertToDataType, out isArray, out isComplexType);

            
    if (convertToDataType == typeof(char))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToCharArray1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToCharArray_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToChar_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToCharArray1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToCharArray(array);
                }
            }
            else
            {
                return (TType)(Object)_ToChar(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(bool))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToBooleanArray1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToBooleanArray_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToBoolean_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToBooleanArray1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToBooleanArray(array);
                }
            }
            else
            {
                return (TType)(Object)_ToBoolean(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(sbyte))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToSByteArray1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToSByteArray_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToSByte_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToSByteArray1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToSByteArray(array);
                }
            }
            else
            {
                return (TType)(Object)_ToSByte(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(byte))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToByteArray1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToByteArray_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToByte_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToByteArray1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToByteArray(array);
                }
            }
            else
            {
                return (TType)(Object)_ToByte(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(ushort))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToUInt16Array1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToUInt16Array_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToUInt16_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToUInt16Array1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToUInt16Array(array);
                }
            }
            else
            {
                return (TType)(Object)_ToUInt16(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(short))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToInt16Array1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToInt16Array_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToInt16_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToInt16Array1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToInt16Array(array);
                }
            }
            else
            {
                return (TType)(Object)_ToInt16(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(uint))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToUInt32Array1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToUInt32Array_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToUInt32_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToUInt32Array1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToUInt32Array(array);
                }
            }
            else
            {
                return (TType)(Object)_ToUInt32(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(int))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToInt32Array1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToInt32Array_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToInt32_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToInt32Array1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToInt32Array(array);
                }
            }
            else
            {
                return (TType)(Object)_ToInt32(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(ulong))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToUInt64Array1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToUInt64Array_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToUInt64_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToUInt64Array1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToUInt64Array(array);
                }
            }
            else
            {
                return (TType)(Object)_ToUInt64(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(long))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToInt64Array1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToInt64Array_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToInt64_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToInt64Array1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToInt64Array(array);
                }
            }
            else
            {
                return (TType)(Object)_ToInt64(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(float))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToSingleArray1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToSingleArray_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToSingle_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToSingleArray1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToSingleArray(array);
                }
            }
            else
            {
                return (TType)(Object)_ToSingle(array);
            }
        }
    }
    else

    if (convertToDataType == typeof(double))
    {
        if (isComplexType)
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToDoubleArray1D_Cplx(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToDoubleArray_Cplx(array);
                }
            }
            else
            {
                return (TType)(Object)_ToDouble_Cplx(array);
            }
        }
        else
        {
            if (isArray)
            {
                int rank = genericType.GetArrayRank();

                if ( (rank == 1) && (!noVectorization) )
                {
                    return (TType)(Object)_ToDoubleArray1D(array);
                }
                else
                {
                    if (rank != ndims)
                    {
                        throw new InvalidCastException(string.Format(
                            "The mxArray have {0} dimensions but the result have {1}, no way to convert", ndims, rank));
                    }
                    return (TType)(Object)_ToDoubleArray(array);
                }
            }
            else
            {
                return (TType)(Object)_ToDouble(array);
            }
        }
    }
    else

                throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, typeof(TType).Name));
        }

        
        #region Convert to Char
        
        unsafe static char _ToChar(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.Char :
                {
                    char* pr;
                    pr = (char*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "char"));
            }
        }

        unsafe static Complex<char> _ToChar_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.Char :
                {
                    char defaultValue;
                    defaultValue = default(char);

                    char* pr, pi;    
                    pr = (char*)array.RealElements;
                    pi = (char*)array.ImaginaryElements;
                    Complex<char> result;
                    
                    result = new Complex<char>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<char>"));
            }
        }

        unsafe static char[] _ToCharArray1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            char[] result = new char[count];
                
            switch(classId)
            {
                
                case ClassID.Char :
                {
                    char* p;
                    p = (char*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "char"));
            }
            
            return result;
        }

        unsafe static Complex<char>[] _ToCharArray1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<char>[] result;
            result = new Complex<char>[count];
                
            switch(classId)
            {
                
                case ClassID.Char :
                {
                    char defaultValue;
                    defaultValue = default(char);

                    char* pr, pi;
                    pr = (char*)array.RealElements;
                    pi = (char*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<char>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "char"));
            }
            
            return result;
        }

        unsafe static Array _ToCharArray(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(char), dims);
            switch(classId)
            {
                
                case ClassID.Char :
                {
                    char* p;
                    p = (char*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "char"));
            }
            
            return result;
        }

        unsafe static Array _ToCharArray_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<char>), dims);
            switch(classId)
            {
                
                case ClassID.Char :
                {
                    char defaultValue;
                    defaultValue = default(char);

                    char* pr, pi;
                    pr = (char*)array.RealElements;
                    pi = (char*)array.ImaginaryElements;
                    Complex<char> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<char>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<char>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to Boolean
        
        unsafe static bool _ToBoolean(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.Logical :
                {
                    bool* pr;
                    pr = (bool*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "bool"));
            }
        }

        unsafe static Complex<bool> _ToBoolean_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.Logical :
                {
                    bool defaultValue;
                    defaultValue = default(bool);

                    bool* pr, pi;    
                    pr = (bool*)array.RealElements;
                    pi = (bool*)array.ImaginaryElements;
                    Complex<bool> result;
                    
                    result = new Complex<bool>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<bool>"));
            }
        }

        unsafe static bool[] _ToBooleanArray1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            bool[] result = new bool[count];
                
            switch(classId)
            {
                
                case ClassID.Logical :
                {
                    bool* p;
                    p = (bool*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "bool"));
            }
            
            return result;
        }

        unsafe static Complex<bool>[] _ToBooleanArray1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<bool>[] result;
            result = new Complex<bool>[count];
                
            switch(classId)
            {
                
                case ClassID.Logical :
                {
                    bool defaultValue;
                    defaultValue = default(bool);

                    bool* pr, pi;
                    pr = (bool*)array.RealElements;
                    pi = (bool*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<bool>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "bool"));
            }
            
            return result;
        }

        unsafe static Array _ToBooleanArray(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(bool), dims);
            switch(classId)
            {
                
                case ClassID.Logical :
                {
                    bool* p;
                    p = (bool*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "bool"));
            }
            
            return result;
        }

        unsafe static Array _ToBooleanArray_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<bool>), dims);
            switch(classId)
            {
                
                case ClassID.Logical :
                {
                    bool defaultValue;
                    defaultValue = default(bool);

                    bool* pr, pi;
                    pr = (bool*)array.RealElements;
                    pi = (bool*)array.ImaginaryElements;
                    Complex<bool> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<bool>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<bool>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to SByte
        
        unsafe static sbyte _ToSByte(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.Int8 :
                {
                    sbyte* pr;
                    pr = (sbyte*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "sbyte"));
            }
        }

        unsafe static Complex<sbyte> _ToSByte_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.Int8 :
                {
                    sbyte defaultValue;
                    defaultValue = default(sbyte);

                    sbyte* pr, pi;    
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<sbyte> result;
                    
                    result = new Complex<sbyte>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<sbyte>"));
            }
        }

        unsafe static sbyte[] _ToSByteArray1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            sbyte[] result = new sbyte[count];
                
            switch(classId)
            {
                
                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "sbyte"));
            }
            
            return result;
        }

        unsafe static Complex<sbyte>[] _ToSByteArray1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<sbyte>[] result;
            result = new Complex<sbyte>[count];
                
            switch(classId)
            {
                
                case ClassID.Int8 :
                {
                    sbyte defaultValue;
                    defaultValue = default(sbyte);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<sbyte>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "sbyte"));
            }
            
            return result;
        }

        unsafe static Array _ToSByteArray(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(sbyte), dims);
            switch(classId)
            {
                
                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "sbyte"));
            }
            
            return result;
        }

        unsafe static Array _ToSByteArray_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<sbyte>), dims);
            switch(classId)
            {
                
                case ClassID.Int8 :
                {
                    sbyte defaultValue;
                    defaultValue = default(sbyte);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<sbyte> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<sbyte>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<sbyte>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to Byte
        
        unsafe static byte _ToByte(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* pr;
                    pr = (byte*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "byte"));
            }
        }

        unsafe static Complex<byte> _ToByte_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte defaultValue;
                    defaultValue = default(byte);

                    byte* pr, pi;    
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<byte> result;
                    
                    result = new Complex<byte>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<byte>"));
            }
        }

        unsafe static byte[] _ToByteArray1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            byte[] result = new byte[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "byte"));
            }
            
            return result;
        }

        unsafe static Complex<byte>[] _ToByteArray1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<byte>[] result;
            result = new Complex<byte>[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte defaultValue;
                    defaultValue = default(byte);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<byte>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "byte"));
            }
            
            return result;
        }

        unsafe static Array _ToByteArray(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(byte), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "byte"));
            }
            
            return result;
        }

        unsafe static Array _ToByteArray_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<byte>), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte defaultValue;
                    defaultValue = default(byte);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<byte> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<byte>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<byte>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to UInt16
        
        unsafe static ushort _ToUInt16(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* pr;
                    pr = (byte*)array.RealElements;                
                    
                    return (ushort)(*pr);
                    
                }

                case ClassID.Int8 :
                {
                    sbyte* pr;
                    pr = (sbyte*)array.RealElements;                
                    
                    return (ushort)(*pr);
                    
                }

                case ClassID.UInt16 :
                {
                    ushort* pr;
                    pr = (ushort*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "ushort"));
            }
        }

        unsafe static Complex<ushort> _ToUInt16_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    ushort defaultValue;
                    defaultValue = default(ushort);

                    byte* pr, pi;    
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<ushort> result;
                    
                    result = new Complex<ushort>(
                        (ushort)(*pr),
                        pi != null ? (ushort)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int8 :
                {
                    ushort defaultValue;
                    defaultValue = default(ushort);

                    sbyte* pr, pi;    
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<ushort> result;
                    
                    result = new Complex<ushort>(
                        (ushort)(*pr),
                        pi != null ? (ushort)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt16 :
                {
                    ushort defaultValue;
                    defaultValue = default(ushort);

                    ushort* pr, pi;    
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<ushort> result;
                    
                    result = new Complex<ushort>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<ushort>"));
            }
        }

        unsafe static ushort[] _ToUInt16Array1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            ushort[] result = new ushort[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (ushort)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (ushort)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "ushort"));
            }
            
            return result;
        }

        unsafe static Complex<ushort>[] _ToUInt16Array1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<ushort>[] result;
            result = new Complex<ushort>[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    ushort defaultValue;
                    defaultValue = default(ushort);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<ushort>(
                            (ushort)(*pr++),
                            pi != null ? (ushort)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    ushort defaultValue;
                    defaultValue = default(ushort);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<ushort>(
                            (ushort)(*pr++),
                            pi != null ? (ushort)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort defaultValue;
                    defaultValue = default(ushort);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<ushort>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "ushort"));
            }
            
            return result;
        }

        unsafe static Array _ToUInt16Array(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(ushort), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((ushort)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((ushort)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "ushort"));
            }
            
            return result;
        }

        unsafe static Array _ToUInt16Array_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<ushort>), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    ushort defaultValue;
                    defaultValue = default(ushort);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<ushort> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<ushort>(
                            (ushort)(*pr++),
                            pi != null ? (ushort)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    ushort defaultValue;
                    defaultValue = default(ushort);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<ushort> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<ushort>(
                            (ushort)(*pr++),
                            pi != null ? (ushort)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort defaultValue;
                    defaultValue = default(ushort);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<ushort> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<ushort>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<ushort>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to Int16
        
        unsafe static short _ToInt16(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* pr;
                    pr = (byte*)array.RealElements;                
                    
                    return (short)(*pr);
                    
                }

                case ClassID.Int8 :
                {
                    sbyte* pr;
                    pr = (sbyte*)array.RealElements;                
                    
                    return (short)(*pr);
                    
                }

                case ClassID.Int16 :
                {
                    short* pr;
                    pr = (short*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "short"));
            }
        }

        unsafe static Complex<short> _ToInt16_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    short defaultValue;
                    defaultValue = default(short);

                    byte* pr, pi;    
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<short> result;
                    
                    result = new Complex<short>(
                        (short)(*pr),
                        pi != null ? (short)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int8 :
                {
                    short defaultValue;
                    defaultValue = default(short);

                    sbyte* pr, pi;    
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<short> result;
                    
                    result = new Complex<short>(
                        (short)(*pr),
                        pi != null ? (short)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int16 :
                {
                    short defaultValue;
                    defaultValue = default(short);

                    short* pr, pi;    
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<short> result;
                    
                    result = new Complex<short>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<short>"));
            }
        }

        unsafe static short[] _ToInt16Array1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            short[] result = new short[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (short)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (short)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "short"));
            }
            
            return result;
        }

        unsafe static Complex<short>[] _ToInt16Array1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<short>[] result;
            result = new Complex<short>[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    short defaultValue;
                    defaultValue = default(short);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<short>(
                            (short)(*pr++),
                            pi != null ? (short)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    short defaultValue;
                    defaultValue = default(short);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<short>(
                            (short)(*pr++),
                            pi != null ? (short)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short defaultValue;
                    defaultValue = default(short);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<short>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "short"));
            }
            
            return result;
        }

        unsafe static Array _ToInt16Array(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(short), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((short)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((short)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "short"));
            }
            
            return result;
        }

        unsafe static Array _ToInt16Array_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<short>), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    short defaultValue;
                    defaultValue = default(short);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<short> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<short>(
                            (short)(*pr++),
                            pi != null ? (short)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    short defaultValue;
                    defaultValue = default(short);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<short> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<short>(
                            (short)(*pr++),
                            pi != null ? (short)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short defaultValue;
                    defaultValue = default(short);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<short> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<short>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<short>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to UInt32
        
        unsafe static uint _ToUInt32(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* pr;
                    pr = (byte*)array.RealElements;                
                    
                    return (uint)(*pr);
                    
                }

                case ClassID.Int8 :
                {
                    sbyte* pr;
                    pr = (sbyte*)array.RealElements;                
                    
                    return (uint)(*pr);
                    
                }

                case ClassID.UInt16 :
                {
                    ushort* pr;
                    pr = (ushort*)array.RealElements;                
                    
                    return (uint)(*pr);
                    
                }

                case ClassID.Int16 :
                {
                    short* pr;
                    pr = (short*)array.RealElements;                
                    
                    return (uint)(*pr);
                    
                }

                case ClassID.UInt32 :
                {
                    uint* pr;
                    pr = (uint*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "uint"));
            }
        }

        unsafe static Complex<uint> _ToUInt32_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    byte* pr, pi;    
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<uint> result;
                    
                    result = new Complex<uint>(
                        (uint)(*pr),
                        pi != null ? (uint)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int8 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    sbyte* pr, pi;    
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<uint> result;
                    
                    result = new Complex<uint>(
                        (uint)(*pr),
                        pi != null ? (uint)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt16 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    ushort* pr, pi;    
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<uint> result;
                    
                    result = new Complex<uint>(
                        (uint)(*pr),
                        pi != null ? (uint)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int16 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    short* pr, pi;    
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<uint> result;
                    
                    result = new Complex<uint>(
                        (uint)(*pr),
                        pi != null ? (uint)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt32 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    uint* pr, pi;    
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                    Complex<uint> result;
                    
                    result = new Complex<uint>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<uint>"));
            }
        }

        unsafe static uint[] _ToUInt32Array1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            uint[] result = new uint[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (uint)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (uint)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (uint)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (uint)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint* p;
                    p = (uint*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "uint"));
            }
            
            return result;
        }

        unsafe static Complex<uint>[] _ToUInt32Array1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<uint>[] result;
            result = new Complex<uint>[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<uint>(
                            (uint)(*pr++),
                            pi != null ? (uint)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<uint>(
                            (uint)(*pr++),
                            pi != null ? (uint)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<uint>(
                            (uint)(*pr++),
                            pi != null ? (uint)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<uint>(
                            (uint)(*pr++),
                            pi != null ? (uint)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    uint* pr, pi;
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<uint>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "uint"));
            }
            
            return result;
        }

        unsafe static Array _ToUInt32Array(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(uint), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((uint)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((uint)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((uint)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((uint)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint* p;
                    p = (uint*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "uint"));
            }
            
            return result;
        }

        unsafe static Array _ToUInt32Array_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<uint>), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<uint> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<uint>(
                            (uint)(*pr++),
                            pi != null ? (uint)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<uint> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<uint>(
                            (uint)(*pr++),
                            pi != null ? (uint)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<uint> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<uint>(
                            (uint)(*pr++),
                            pi != null ? (uint)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<uint> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<uint>(
                            (uint)(*pr++),
                            pi != null ? (uint)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint defaultValue;
                    defaultValue = default(uint);

                    uint* pr, pi;
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                    Complex<uint> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<uint>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<uint>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to Int32
        
        unsafe static int _ToInt32(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* pr;
                    pr = (byte*)array.RealElements;                
                    
                    return (int)(*pr);
                    
                }

                case ClassID.Int8 :
                {
                    sbyte* pr;
                    pr = (sbyte*)array.RealElements;                
                    
                    return (int)(*pr);
                    
                }

                case ClassID.UInt16 :
                {
                    ushort* pr;
                    pr = (ushort*)array.RealElements;                
                    
                    return (int)(*pr);
                    
                }

                case ClassID.Int16 :
                {
                    short* pr;
                    pr = (short*)array.RealElements;                
                    
                    return (int)(*pr);
                    
                }

                case ClassID.Int32 :
                {
                    int* pr;
                    pr = (int*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "int"));
            }
        }

        unsafe static Complex<int> _ToInt32_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    byte* pr, pi;    
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<int> result;
                    
                    result = new Complex<int>(
                        (int)(*pr),
                        pi != null ? (int)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int8 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    sbyte* pr, pi;    
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<int> result;
                    
                    result = new Complex<int>(
                        (int)(*pr),
                        pi != null ? (int)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt16 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    ushort* pr, pi;    
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<int> result;
                    
                    result = new Complex<int>(
                        (int)(*pr),
                        pi != null ? (int)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int16 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    short* pr, pi;    
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<int> result;
                    
                    result = new Complex<int>(
                        (int)(*pr),
                        pi != null ? (int)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int32 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    int* pr, pi;    
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                    Complex<int> result;
                    
                    result = new Complex<int>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<int>"));
            }
        }

        unsafe static int[] _ToInt32Array1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            int[] result = new int[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (int)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (int)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (int)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (int)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int* p;
                    p = (int*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "int"));
            }
            
            return result;
        }

        unsafe static Complex<int>[] _ToInt32Array1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<int>[] result;
            result = new Complex<int>[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<int>(
                            (int)(*pr++),
                            pi != null ? (int)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<int>(
                            (int)(*pr++),
                            pi != null ? (int)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<int>(
                            (int)(*pr++),
                            pi != null ? (int)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<int>(
                            (int)(*pr++),
                            pi != null ? (int)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    int* pr, pi;
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<int>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "int"));
            }
            
            return result;
        }

        unsafe static Array _ToInt32Array(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(int), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((int)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((int)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((int)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((int)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int* p;
                    p = (int*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "int"));
            }
            
            return result;
        }

        unsafe static Array _ToInt32Array_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<int>), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<int> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<int>(
                            (int)(*pr++),
                            pi != null ? (int)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<int> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<int>(
                            (int)(*pr++),
                            pi != null ? (int)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<int> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<int>(
                            (int)(*pr++),
                            pi != null ? (int)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<int> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<int>(
                            (int)(*pr++),
                            pi != null ? (int)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int defaultValue;
                    defaultValue = default(int);

                    int* pr, pi;
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                    Complex<int> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<int>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<int>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to UInt64
        
        unsafe static ulong _ToUInt64(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* pr;
                    pr = (byte*)array.RealElements;                
                    
                    return (ulong)(*pr);
                    
                }

                case ClassID.Int8 :
                {
                    sbyte* pr;
                    pr = (sbyte*)array.RealElements;                
                    
                    return (ulong)(*pr);
                    
                }

                case ClassID.UInt16 :
                {
                    ushort* pr;
                    pr = (ushort*)array.RealElements;                
                    
                    return (ulong)(*pr);
                    
                }

                case ClassID.Int16 :
                {
                    short* pr;
                    pr = (short*)array.RealElements;                
                    
                    return (ulong)(*pr);
                    
                }

                case ClassID.UInt32 :
                {
                    uint* pr;
                    pr = (uint*)array.RealElements;                
                    
                    return (ulong)(*pr);
                    
                }

                case ClassID.Int32 :
                {
                    int* pr;
                    pr = (int*)array.RealElements;                
                    
                    return (ulong)(*pr);
                    
                }

                case ClassID.UInt64 :
                {
                    ulong* pr;
                    pr = (ulong*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "ulong"));
            }
        }

        unsafe static Complex<ulong> _ToUInt64_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    byte* pr, pi;    
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<ulong> result;
                    
                    result = new Complex<ulong>(
                        (ulong)(*pr),
                        pi != null ? (ulong)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int8 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    sbyte* pr, pi;    
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<ulong> result;
                    
                    result = new Complex<ulong>(
                        (ulong)(*pr),
                        pi != null ? (ulong)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt16 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    ushort* pr, pi;    
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<ulong> result;
                    
                    result = new Complex<ulong>(
                        (ulong)(*pr),
                        pi != null ? (ulong)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int16 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    short* pr, pi;    
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<ulong> result;
                    
                    result = new Complex<ulong>(
                        (ulong)(*pr),
                        pi != null ? (ulong)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt32 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    uint* pr, pi;    
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                    Complex<ulong> result;
                    
                    result = new Complex<ulong>(
                        (ulong)(*pr),
                        pi != null ? (ulong)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int32 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    int* pr, pi;    
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                    Complex<ulong> result;
                    
                    result = new Complex<ulong>(
                        (ulong)(*pr),
                        pi != null ? (ulong)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt64 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    ulong* pr, pi;    
                    pr = (ulong*)array.RealElements;
                    pi = (ulong*)array.ImaginaryElements;
                    Complex<ulong> result;
                    
                    result = new Complex<ulong>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<ulong>"));
            }
        }

        unsafe static ulong[] _ToUInt64Array1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            ulong[] result = new ulong[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (ulong)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (ulong)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (ulong)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (ulong)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint* p;
                    p = (uint*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (ulong)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int* p;
                    p = (int*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (ulong)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    ulong* p;
                    p = (ulong*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "ulong"));
            }
            
            return result;
        }

        unsafe static Complex<ulong>[] _ToUInt64Array1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<ulong>[] result;
            result = new Complex<ulong>[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    uint* pr, pi;
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    int* pr, pi;
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    ulong* pr, pi;
                    pr = (ulong*)array.RealElements;
                    pi = (ulong*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<ulong>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "ulong"));
            }
            
            return result;
        }

        unsafe static Array _ToUInt64Array(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(ulong), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((ulong)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((ulong)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((ulong)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((ulong)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint* p;
                    p = (uint*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((ulong)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int* p;
                    p = (int*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((ulong)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    ulong* p;
                    p = (ulong*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "ulong"));
            }
            
            return result;
        }

        unsafe static Array _ToUInt64Array_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<ulong>), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<ulong> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<ulong> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<ulong> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<ulong> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    uint* pr, pi;
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                    Complex<ulong> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    int* pr, pi;
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                    Complex<ulong> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<ulong>(
                            (ulong)(*pr++),
                            pi != null ? (ulong)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    ulong defaultValue;
                    defaultValue = default(ulong);

                    ulong* pr, pi;
                    pr = (ulong*)array.RealElements;
                    pi = (ulong*)array.ImaginaryElements;
                    Complex<ulong> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<ulong>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<ulong>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to Int64
        
        unsafe static long _ToInt64(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* pr;
                    pr = (byte*)array.RealElements;                
                    
                    return (long)(*pr);
                    
                }

                case ClassID.Int8 :
                {
                    sbyte* pr;
                    pr = (sbyte*)array.RealElements;                
                    
                    return (long)(*pr);
                    
                }

                case ClassID.UInt16 :
                {
                    ushort* pr;
                    pr = (ushort*)array.RealElements;                
                    
                    return (long)(*pr);
                    
                }

                case ClassID.Int16 :
                {
                    short* pr;
                    pr = (short*)array.RealElements;                
                    
                    return (long)(*pr);
                    
                }

                case ClassID.UInt32 :
                {
                    uint* pr;
                    pr = (uint*)array.RealElements;                
                    
                    return (long)(*pr);
                    
                }

                case ClassID.Int32 :
                {
                    int* pr;
                    pr = (int*)array.RealElements;                
                    
                    return (long)(*pr);
                    
                }

                case ClassID.Int64 :
                {
                    long* pr;
                    pr = (long*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "long"));
            }
        }

        unsafe static Complex<long> _ToInt64_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    byte* pr, pi;    
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<long> result;
                    
                    result = new Complex<long>(
                        (long)(*pr),
                        pi != null ? (long)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int8 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    sbyte* pr, pi;    
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<long> result;
                    
                    result = new Complex<long>(
                        (long)(*pr),
                        pi != null ? (long)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt16 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    ushort* pr, pi;    
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<long> result;
                    
                    result = new Complex<long>(
                        (long)(*pr),
                        pi != null ? (long)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int16 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    short* pr, pi;    
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<long> result;
                    
                    result = new Complex<long>(
                        (long)(*pr),
                        pi != null ? (long)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt32 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    uint* pr, pi;    
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                    Complex<long> result;
                    
                    result = new Complex<long>(
                        (long)(*pr),
                        pi != null ? (long)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int32 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    int* pr, pi;    
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                    Complex<long> result;
                    
                    result = new Complex<long>(
                        (long)(*pr),
                        pi != null ? (long)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int64 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    long* pr, pi;    
                    pr = (long*)array.RealElements;
                    pi = (long*)array.ImaginaryElements;
                    Complex<long> result;
                    
                    result = new Complex<long>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<long>"));
            }
        }

        unsafe static long[] _ToInt64Array1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            long[] result = new long[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (long)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (long)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (long)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (long)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint* p;
                    p = (uint*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (long)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int* p;
                    p = (int*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (long)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    long* p;
                    p = (long*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "long"));
            }
            
            return result;
        }

        unsafe static Complex<long>[] _ToInt64Array1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<long>[] result;
            result = new Complex<long>[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    uint* pr, pi;
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    int* pr, pi;
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    long* pr, pi;
                    pr = (long*)array.RealElements;
                    pi = (long*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<long>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "long"));
            }
            
            return result;
        }

        unsafe static Array _ToInt64Array(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(long), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((long)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((long)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((long)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((long)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint* p;
                    p = (uint*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((long)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int* p;
                    p = (int*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((long)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    long* p;
                    p = (long*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "long"));
            }
            
            return result;
        }

        unsafe static Array _ToInt64Array_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<long>), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<long> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<long> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<long> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<long> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    uint* pr, pi;
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                    Complex<long> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    int* pr, pi;
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                    Complex<long> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<long>(
                            (long)(*pr++),
                            pi != null ? (long)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    long defaultValue;
                    defaultValue = default(long);

                    long* pr, pi;
                    pr = (long*)array.RealElements;
                    pi = (long*)array.ImaginaryElements;
                    Complex<long> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<long>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<long>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to Single
        
        unsafe static float _ToSingle(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* pr;
                    pr = (byte*)array.RealElements;                
                    
                    return (float)(*pr);
                    
                }

                case ClassID.Int8 :
                {
                    sbyte* pr;
                    pr = (sbyte*)array.RealElements;                
                    
                    return (float)(*pr);
                    
                }

                case ClassID.UInt16 :
                {
                    ushort* pr;
                    pr = (ushort*)array.RealElements;                
                    
                    return (float)(*pr);
                    
                }

                case ClassID.Int16 :
                {
                    short* pr;
                    pr = (short*)array.RealElements;                
                    
                    return (float)(*pr);
                    
                }

                case ClassID.UInt32 :
                {
                    uint* pr;
                    pr = (uint*)array.RealElements;                
                    
                    return (float)(*pr);
                    
                }

                case ClassID.Int32 :
                {
                    int* pr;
                    pr = (int*)array.RealElements;                
                    
                    return (float)(*pr);
                    
                }

                case ClassID.UInt64 :
                {
                    ulong* pr;
                    pr = (ulong*)array.RealElements;                
                    
                    return (float)(*pr);
                    
                }

                case ClassID.Int64 :
                {
                    long* pr;
                    pr = (long*)array.RealElements;                
                    
                    return (float)(*pr);
                    
                }

                case ClassID.Single :
                {
                    float* pr;
                    pr = (float*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "float"));
            }
        }

        unsafe static Complex<float> _ToSingle_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    byte* pr, pi;    
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<float> result;
                    
                    result = new Complex<float>(
                        (float)(*pr),
                        pi != null ? (float)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int8 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    sbyte* pr, pi;    
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<float> result;
                    
                    result = new Complex<float>(
                        (float)(*pr),
                        pi != null ? (float)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt16 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    ushort* pr, pi;    
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<float> result;
                    
                    result = new Complex<float>(
                        (float)(*pr),
                        pi != null ? (float)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int16 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    short* pr, pi;    
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<float> result;
                    
                    result = new Complex<float>(
                        (float)(*pr),
                        pi != null ? (float)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt32 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    uint* pr, pi;    
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                    Complex<float> result;
                    
                    result = new Complex<float>(
                        (float)(*pr),
                        pi != null ? (float)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int32 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    int* pr, pi;    
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                    Complex<float> result;
                    
                    result = new Complex<float>(
                        (float)(*pr),
                        pi != null ? (float)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt64 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    ulong* pr, pi;    
                    pr = (ulong*)array.RealElements;
                    pi = (ulong*)array.ImaginaryElements;
                    Complex<float> result;
                    
                    result = new Complex<float>(
                        (float)(*pr),
                        pi != null ? (float)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int64 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    long* pr, pi;    
                    pr = (long*)array.RealElements;
                    pi = (long*)array.ImaginaryElements;
                    Complex<float> result;
                    
                    result = new Complex<float>(
                        (float)(*pr),
                        pi != null ? (float)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Single :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    float* pr, pi;    
                    pr = (float*)array.RealElements;
                    pi = (float*)array.ImaginaryElements;
                    Complex<float> result;
                    
                    result = new Complex<float>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<float>"));
            }
        }

        unsafe static float[] _ToSingleArray1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            float[] result = new float[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (float)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (float)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (float)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (float)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint* p;
                    p = (uint*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (float)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int* p;
                    p = (int*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (float)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    ulong* p;
                    p = (ulong*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (float)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    long* p;
                    p = (long*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (float)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Single :
                {
                    float* p;
                    p = (float*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "float"));
            }
            
            return result;
        }

        unsafe static Complex<float>[] _ToSingleArray1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<float>[] result;
            result = new Complex<float>[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    uint* pr, pi;
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    int* pr, pi;
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    ulong* pr, pi;
                    pr = (ulong*)array.RealElements;
                    pi = (ulong*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    long* pr, pi;
                    pr = (long*)array.RealElements;
                    pi = (long*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Single :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    float* pr, pi;
                    pr = (float*)array.RealElements;
                    pi = (float*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<float>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "float"));
            }
            
            return result;
        }

        unsafe static Array _ToSingleArray(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(float), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((float)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((float)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((float)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((float)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint* p;
                    p = (uint*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((float)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int* p;
                    p = (int*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((float)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    ulong* p;
                    p = (ulong*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((float)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    long* p;
                    p = (long*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((float)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Single :
                {
                    float* p;
                    p = (float*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "float"));
            }
            
            return result;
        }

        unsafe static Array _ToSingleArray_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<float>), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<float> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<float> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<float> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<float> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    uint* pr, pi;
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                    Complex<float> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    int* pr, pi;
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                    Complex<float> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    ulong* pr, pi;
                    pr = (ulong*)array.RealElements;
                    pi = (ulong*)array.ImaginaryElements;
                    Complex<float> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    long* pr, pi;
                    pr = (long*)array.RealElements;
                    pi = (long*)array.ImaginaryElements;
                    Complex<float> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<float>(
                            (float)(*pr++),
                            pi != null ? (float)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Single :
                {
                    float defaultValue;
                    defaultValue = default(float);

                    float* pr, pi;
                    pr = (float*)array.RealElements;
                    pi = (float*)array.ImaginaryElements;
                    Complex<float> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<float>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<float>"));
            }                
            
            return result;
        }

        #endregion

        #region Convert to Double
        
        unsafe static double _ToDouble(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* pr;
                    pr = (byte*)array.RealElements;                
                    
                    return (double)(*pr);
                    
                }

                case ClassID.Int8 :
                {
                    sbyte* pr;
                    pr = (sbyte*)array.RealElements;                
                    
                    return (double)(*pr);
                    
                }

                case ClassID.UInt16 :
                {
                    ushort* pr;
                    pr = (ushort*)array.RealElements;                
                    
                    return (double)(*pr);
                    
                }

                case ClassID.Int16 :
                {
                    short* pr;
                    pr = (short*)array.RealElements;                
                    
                    return (double)(*pr);
                    
                }

                case ClassID.UInt32 :
                {
                    uint* pr;
                    pr = (uint*)array.RealElements;                
                    
                    return (double)(*pr);
                    
                }

                case ClassID.Int32 :
                {
                    int* pr;
                    pr = (int*)array.RealElements;                
                    
                    return (double)(*pr);
                    
                }

                case ClassID.UInt64 :
                {
                    ulong* pr;
                    pr = (ulong*)array.RealElements;                
                    
                    return (double)(*pr);
                    
                }

                case ClassID.Int64 :
                {
                    long* pr;
                    pr = (long*)array.RealElements;                
                    
                    return (double)(*pr);
                    
                }

                case ClassID.Single :
                {
                    float* pr;
                    pr = (float*)array.RealElements;                
                    
                    return (double)(*pr);
                    
                }

                case ClassID.Double :
                {
                    double* pr;
                    pr = (double*)array.RealElements;                
                    
                    return *pr;
                    
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId, "double"));
            }
        }

        unsafe static Complex<double> _ToDouble_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            if (array.NumberOfElements != 1)
                throw new InvalidCastException(string.Format(ONE_ELEMENT_REQUIRED, array.ToString()));
            ClassID classId = array.Class;
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    byte* pr, pi;    
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<double> result;
                    
                    result = new Complex<double>(
                        (double)(*pr),
                        pi != null ? (double)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int8 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    sbyte* pr, pi;    
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<double> result;
                    
                    result = new Complex<double>(
                        (double)(*pr),
                        pi != null ? (double)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt16 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    ushort* pr, pi;    
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<double> result;
                    
                    result = new Complex<double>(
                        (double)(*pr),
                        pi != null ? (double)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int16 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    short* pr, pi;    
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<double> result;
                    
                    result = new Complex<double>(
                        (double)(*pr),
                        pi != null ? (double)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt32 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    uint* pr, pi;    
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                    Complex<double> result;
                    
                    result = new Complex<double>(
                        (double)(*pr),
                        pi != null ? (double)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int32 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    int* pr, pi;    
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                    Complex<double> result;
                    
                    result = new Complex<double>(
                        (double)(*pr),
                        pi != null ? (double)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.UInt64 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    ulong* pr, pi;    
                    pr = (ulong*)array.RealElements;
                    pi = (ulong*)array.ImaginaryElements;
                    Complex<double> result;
                    
                    result = new Complex<double>(
                        (double)(*pr),
                        pi != null ? (double)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Int64 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    long* pr, pi;    
                    pr = (long*)array.RealElements;
                    pi = (long*)array.ImaginaryElements;
                    Complex<double> result;
                    
                    result = new Complex<double>(
                        (double)(*pr),
                        pi != null ? (double)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Single :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    float* pr, pi;    
                    pr = (float*)array.RealElements;
                    pi = (float*)array.ImaginaryElements;
                    Complex<double> result;
                    
                    result = new Complex<double>(
                        (double)(*pr),
                        pi != null ? (double)(*pi) : defaultValue);
                    
                    return result;
                }

                case ClassID.Double :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    double* pr, pi;    
                    pr = (double*)array.RealElements;
                    pi = (double*)array.ImaginaryElements;
                    Complex<double> result;
                    
                    result = new Complex<double>(*pr,
                        pi != null ? *pi : defaultValue);
                    
                    return result;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<double>"));
            }
        }

        unsafe static double[] _ToDoubleArray1D(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            double[] result = new double[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (double)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (double)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (double)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (double)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint* p;
                    p = (uint*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (double)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int* p;
                    p = (int*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (double)(*p++);
                        
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    ulong* p;
                    p = (ulong*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (double)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    long* p;
                    p = (long*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (double)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Single :
                {
                    float* p;
                    p = (float*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = (double)(*p++);
                        
                    }
                    break;
                }

                case ClassID.Double :
                {
                    double* p;
                    p = (double*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = *p++;
                        
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "double"));
            }
            
            return result;
        }

        unsafe static Complex<double>[] _ToDoubleArray1D_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            ClassID classId = array.Class;
            Complex<double>[] result;
            result = new Complex<double>[count];
                
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    uint* pr, pi;
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    int* pr, pi;
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    ulong* pr, pi;
                    pr = (ulong*)array.RealElements;
                    pi = (ulong*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    long* pr, pi;
                    pr = (long*)array.RealElements;
                    pi = (long*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Single :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    float* pr, pi;
                    pr = (float*)array.RealElements;
                    pi = (float*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                                            
                    }
                    break;
                }

                case ClassID.Double :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    double* pr, pi;
                    pr = (double*)array.RealElements;
                    pi = (double*)array.ImaginaryElements;
                   
                    for(int i = 0 ; i < count; i++)
                    {
                        
                        result[i] = new Complex<double>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                                            
                    }
                    break;
                }

                default:
                    throw new InvalidCastException(string.Format(NO_WAY_TO_CAST, classId, "double"));
            }
            
            return result;
        }

        unsafe static Array _ToDoubleArray(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(double), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    byte* p;
                    p = (byte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((double)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    sbyte* p;
                    p = (sbyte*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((double)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    ushort* p;
                    p = (ushort*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((double)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    short* p;
                    p = (short*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((double)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    uint* p;
                    p = (uint*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((double)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    int* p;
                    p = (int*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((double)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    ulong* p;
                    p = (ulong*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((double)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    long* p;
                    p = (long*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((double)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Single :
                {
                    float* p;
                    p = (float*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue((double)(*p++), coords);
                        
                    }
                    break;
                }

                case ClassID.Double :
                {
                    double* p;
                    p = (double*)array.RealElements;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        result.SetValue(*p++, coords);
                        
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "double"));
            }
            
            return result;
        }

        unsafe static Array _ToDoubleArray_Cplx(MxArray array)
        {
            if (array == null) throw new ArgumentNullException("array");
            int count = array.NumberOfElements;
            int[] dims = array.Dimensions;
            Array result;
            ClassID classId = array.Class;
                
            int[] coords;

            result = Array.CreateInstance(typeof(Complex<double>), dims);
            switch(classId)
            {
                
                case ClassID.UInt8 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    byte* pr, pi;
                    pr = (byte*)array.RealElements;
                    pi = (byte*)array.ImaginaryElements;
                    Complex<double> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int8 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    sbyte* pr, pi;
                    pr = (sbyte*)array.RealElements;
                    pi = (sbyte*)array.ImaginaryElements;
                    Complex<double> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt16 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    ushort* pr, pi;
                    pr = (ushort*)array.RealElements;
                    pi = (ushort*)array.ImaginaryElements;
                    Complex<double> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int16 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    short* pr, pi;
                    pr = (short*)array.RealElements;
                    pi = (short*)array.ImaginaryElements;
                    Complex<double> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt32 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    uint* pr, pi;
                    pr = (uint*)array.RealElements;
                    pi = (uint*)array.ImaginaryElements;
                    Complex<double> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int32 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    int* pr, pi;
                    pr = (int*)array.RealElements;
                    pi = (int*)array.ImaginaryElements;
                    Complex<double> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.UInt64 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    ulong* pr, pi;
                    pr = (ulong*)array.RealElements;
                    pi = (ulong*)array.ImaginaryElements;
                    Complex<double> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Int64 :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    long* pr, pi;
                    pr = (long*)array.RealElements;
                    pi = (long*)array.ImaginaryElements;
                    Complex<double> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Single :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    float* pr, pi;
                    pr = (float*)array.RealElements;
                    pi = (float*)array.ImaginaryElements;
                    Complex<double> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<double>(
                            (double)(*pr++),
                            pi != null ? (double)(*pi++) : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                case ClassID.Double :
                {
                    double defaultValue;
                    defaultValue = default(double);

                    double* pr, pi;
                    pr = (double*)array.RealElements;
                    pi = (double*)array.ImaginaryElements;
                    Complex<double> element;
                    for(int i = 0 ; i < count; i++)
                    {
                        //TODO: Optimize the coordinate table generation by not re-calculating it every time.
                        coords = MxUtils.CoordinatesFromIndex(i, dims);
                        
                        element = new Complex<double>(*pr++,
                            pi != null ? *pi++ : defaultValue);
                        
                        result.SetValue(element, coords);
                    }
                    break;
                }

                default:
                    throw new Exception(string.Format(NO_WAY_TO_CAST, classId,
                        "Complex<double>"));
            }                
            
            return result;
        }

        #endregion

    }
}
